//
//  ZYCCicleFriendsDetailPhotoViewController.m
//  ZDSC
//
//  Created by ZDTX on 2021/9/19.
//

#import "ZYCCicleFriendsDetailPhotoViewController.h"
#import "NSDateExtra.h"
#import "ZYCCycleFriendDetailModel.h"
#import "ZYCCycleFriendModel.h"
#import "ZYCLoginOutView.h"
#import "ZYCCircleFriendsColletionView.h"

static NSInteger pageSize = 10;


@interface ZYCCicleFriendsDetailPhotoViewController ()<ZYCLoginOutViewDelegate,ZYCCircleFriendsColletionViewDelegete>

@property (nonatomic, strong) ZYCCircleFriendsColletionView *paperCollectionView;
@property (nonatomic, strong) ZYCLoginOutView *loginOutView;
@property (nonatomic, strong) NSMutableArray *dataSource;
@property (nonatomic, strong) UIButton *deleteButton;

@property (nonatomic, assign) NSInteger currentPage;


@end

@implementation ZYCCicleFriendsDetailPhotoViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = [NSDateExtra getDateStringWithDateString:self.model.uploadTime currenDateFormat:@"yyyy-MM-dd HH:mm:ss" dateFormat:@"yyyy年MM月dd日 HH:mm"];
    [self loadNewData];
    
    [self.view addSubview:self.paperCollectionView];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.deleteButton];
    
    self.paperCollectionView.uploadUserName = self.model.uploadUserName;

}




// 成长档案详情
- (void)loadNewData{

    NSDictionary *param = @{
        @"stuUserId":self.model.stuUserId,
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize)
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_GrowthDetail requestService:RequestServiceWorkBench parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCCycleFriendDetailModel class] json:responseObject[@"body"][@"list"]];
                [self.dataSource removeAllObjects];
                [self.dataSource addObjectsFromArray:array];
                
                self.paperCollectionView.dataArray = self.dataSource;
                [self.paperCollectionView reloadData];
                [self.paperCollectionView.mj_header endRefreshing];
                // 滚动到指定页
                NSInteger position = [self getCurrentPosition:self.dataSource];
                self.currentPage = position;
                [self.paperCollectionView setContentOffset:CGPointMake((self.currentPage) * self.paperCollectionView.frame.size.width, 0) animated:NO];
            }
        } failure:^(NSError * _Nonnull error) {
            [self.paperCollectionView.mj_header endRefreshing];
        }];
}



- (void)loadMoreData{
    self.pageNum ++;
    NSDictionary *param = @{
        @"stuUserId":self.model.stuUserId,
        @"pageNum":@(self.pageNum),
        @"pageSize":@(pageSize)
    };
    [MrjSVProgressHUD showWithStatus:@"加载中..."];
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_GrowthDetail requestService:RequestServiceWorkBench parameters:param progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            [MrjSVProgressHUD dismiss];
            if (SUCCESS(responseObject[@"code"])) {
                NSArray *array = [NSArray yy_modelArrayWithClass:[ZYCCycleFriendDetailModel class] json:responseObject[@"body"][@"list"]];
    
                if (array.count == 0) {
                    [self.paperCollectionView.mj_footer endRefreshingWithNoMoreData];
                }else{
                    [self.dataSource removeAllObjects];
                    [self.dataSource addObjectsFromArray:array];
                    self.paperCollectionView.dataArray = self.dataSource;
                    [self.paperCollectionView reloadData];
                    [self.paperCollectionView.mj_footer endRefreshing];
                }
            }
        } failure:^(NSError * _Nonnull error) {
            [self.paperCollectionView.mj_footer endRefreshing];
        }];
}


#pragma mark - ZYCCircleFriendsColletionViewDelegete
-(void) paperCollectionViewScrollPage:(NSUInteger)page{
    if (self.dataSource.count == 0) {
        return;
    }

    
    ZYCCycleFriendDetailModel * model = self.dataSource[page];
    self.title = [NSDateExtra getDateStringWithDateString:model.uploadTime currenDateFormat:@"yyyy-MM-dd HH:mm:ss" dateFormat:@"yyyy年MM月dd日 HH:mm"];
}

#pragma mark - action
- (void)clickDeleteAction:(UIButton *)sender{
    [self.loginOutView show];
}


#pragma mark - private method
- (NSInteger)getCurrentPosition:(NSArray <ZYCCycleFriendDetailModel *>*)array{
    NSInteger position = 0;
    for (NSInteger i = 0; i < array.count; i++) {
        ZYCCycleFriendDetailModel *model = array[i];
        if ([model.growReportId integerValue] == [self.model.growReportId integerValue]) {
            position = i;
            break;
        }
    }
//    NSLog(@"索引:%zd",position);
    return position;
}

#pragma mark - ZYCLoginOutViewDelegate

- (void)loginOutViewDidClickLogout{
    NSDictionary *params = @{
        @"growReportId":self.model.growReportId
    };
    [[URLManager sharedURLManager] postUrlString:Appliance_Post_GrowthDelete requestService:RequestServiceWorkBench parameters:params progress:^(NSProgress * _Nonnull downloadProgress) {
            
        } success:^(id  _Nullable responseObject) {
            if (SUCCESS(responseObject[@"code"])) {
                [MrjToast makeToast:responseObject[@"body"] makeView:self.view];
                // 滚动到指定页
                NSInteger position = [self getCurrentPosition:self.dataSource];
                [self.dataSource enumerateObjectsUsingBlock:^(ZYCCycleFriendDetailModel *obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    if ([obj.growReportId integerValue] == [self.model.growReportId integerValue]) {
                        [self.dataSource removeObject:obj];
                    }
                }];
                [self.paperCollectionView reloadData];
                if (position == 0) {
                    [self.paperCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:position inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
                }else{
                    [self.paperCollectionView scrollToItemAtIndexPath:[NSIndexPath indexPathForItem:position - 1 inSection:0] atScrollPosition:UICollectionViewScrollPositionNone animated:NO];
                }
            }else{
                [MrjToast makeToast:responseObject[@"message"] makeView:self.view];
            }
        } failure:^(NSError * _Nonnull error) {
            
        }];
}


- (ZYCLoginOutView *)loginOutView {
    if (!_loginOutView) {
        _loginOutView = [[ZYCLoginOutView alloc] initWithFrame:CGRectMake(0, 0, 280.0f, 155.0f)];
        _loginOutView.delegate = self;
        _loginOutView.title = @"温馨提示";
        _loginOutView.content = @"要删除该成长记录吗?";
        [self.navigationController.view addSubview:_loginOutView];
    }
    return _loginOutView;
}


- (NSMutableArray *)dataSource {
    if (!_dataSource) {
        _dataSource = [NSMutableArray arrayWithCapacity:0];
    }
    return _dataSource;
}


- (ZYCCircleFriendsColletionView *)paperCollectionView{
    if (!_paperCollectionView) {
        _paperCollectionView = [[ZYCCircleFriendsColletionView alloc] initWithFrame:CGRectMake(0, kNavBarAndStatusBarHeight, kScreenWidth, kScreenHeight - kNavBarAndStatusBarHeight)];
        _paperCollectionView.backgroundColor = HEX_RGB(@"FFFFFF");
        _paperCollectionView.pDelegate = self;
        // 左滑刷新
        _paperCollectionView.mj_trailer = [MJRefreshNormalTrailer trailerWithRefreshingTarget:self refreshingAction:@selector(loadMoreData)];
    }
    return _paperCollectionView;
}



- (UIButton *)deleteButton{
    if (!_deleteButton) {
        _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_deleteButton setTitle:@"删除" forState:UIControlStateNormal];
        [_deleteButton setTitleColor:HEX_RGB(@"FFFFFF") forState:UIControlStateNormal];
        [_deleteButton addTarget:self action:@selector(clickDeleteAction:) forControlEvents:UIControlEventTouchUpInside];
        _deleteButton.titleLabel.font  = kCustomFont(@"PingFangSC-Regular", 18.0f);
    }
    return _deleteButton;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
